package com.wlcj.upload.web;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jodd.datetime.JDateTime;

import org.apache.commons.io.FileUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.upload.UploadFile;
import com.wlcj.upload.util.Lang;

@SuppressWarnings({"rawtypes", "unchecked"})
public class IndexAction extends Controller {
	
	static Prop prop = PropKit.use("config.properties");
	static List<String> imageSuffixArray = Arrays.asList(new String[]{"jpg", "png", "gif", "jpeg"});
	
	public void index() {
		renderJsp("/index.jsp");
	}
	
	@Before(POST.class)
	public void batchUpload() {
		List<UploadFile> list = getFiles("temp");
		String index1 = getPara("index1", "default");
		String index2 = getPara("index2", "default");
		
		String prefix = prop.get("path.prefix");
		String domain = prop.get("static.domain");
		
		String dir = prefix+index1+"/"+index2+"/";
		String url = domain+index1+"/"+index2+"/";
		JSONArray jsonArray = new JSONArray();
		for(UploadFile file:list) {
			String fileName = file.getFileName();
			String[] array = fileName.split("\\.");
			String suffix = array[array.length-1];
			String newName = Lang.getUUID()+"."+suffix;;
			String path = dir+newName;
			url = url+newName;
			File target = new File(path);
			JSONObject json = new JSONObject();
			try {
				FileUtils.moveFile(file.getFile(), target);
				json.put("name", file.getParameterName());
				json.put("url", url);
				jsonArray.add(json);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		renderJson(jsonArray.toString());
	}
	
	public void listFiles() {
		String prefix = prop.get("path.prefix");
		String domain = prop.get("static.domain");
		String path = getPara("path", prefix);
		File file = new File(path);
		File[] files = file.listFiles();
		List list = new ArrayList();
		for(File f:files) {
			Map map = new HashMap();
			String name = f.getName();
			map.put("name", name);
			boolean flag = f.isDirectory();
			map.put("isDirectory", flag);
			String[] names = name.split("\\.");
			String suffix = names[names.length-1];
			String fullPath = f.getAbsolutePath();
			if(flag) 
				map.put("url", "/listFiles?path="+fullPath);
			else if(imageSuffixArray.contains(suffix))
				map.put("url", domain+Lang.subFilePath(fullPath, prefix));
			else 
				map.put("url", "/download?path="+fullPath);
			map.put("time", new JDateTime(f.lastModified()).toString("YYYY-MM-DD hh:mm:ss"));
		    if(!flag)
		    	map.put("size", Lang.getFormatSize((double)f.length()));
			list.add(map);
		}
		setAttr("list", list);
		setAttr("path", path);
		if(!Lang.isSameDirectory(path, prefix)) {
			System.err.println("传入的path不是根路径");
			setAttr("parent", file.getParent());
		}
		renderJsp("fileList.jsp");
	}
	
	public void download() {
		String path = getPara("path");
		File file = new File(path);
		renderFile(file);
	}
	
	public static void main(String[] args) {
		File src = new File("C:/installlog.txt");
		File dest = new File("C:/Users/cuihuan/Desktop/abc.txt");
		try {
			FileUtils.copyFile(src, dest);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
